package cn.dankal.flyaudio.personal;

import android.view.View;

import com.lihang.ShadowLayout;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;
import api.UserServiceFactory;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.UserInfoBean;
import cn.dankal.basiclib.rx.AbstractDialogSubscriber;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.friend.pickimage.StringUtil;

/**
 * <pre>
 *     e-mail : junfeng.nie@duntech.com.cn
 *     time   : 27/8/2020
 *     desc   :
 *     version: cn.dankal.flyaudio.personal
 *     Copyright: Copyright（c）2018
 *     Company:
 * </pre>
 *
 * @author niejunfeng
 */

public class InviteFriendsActivity extends BaseActivity {
    private AppCompatTextView tvPrompt;
    private AppCompatEditText editPhone;
    private ShadowLayout tvConfirm;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_invite_friends;
    }

    @Override
    protected void initComponents() {
        tvConfirm = findViewById(R.id.shadow_confirm);
        tvPrompt = findViewById(R.id.tv_prompt);
        editPhone = findViewById(R.id.edit_phone);


        findViewById(R.id.iv_onback).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                }
        );

        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getUser();
            }
        });
    }

    private void getUser(){
        String phone = editPhone.getText().toString().trim();
        if(StringUtil.isEmpty(phone)){
            ToastUtils.showShort("请输入手机号");
            return;
        }
        if (!cn.dankal.basiclib.util.StringUtil.checkPhone(phone)) {
            ToastUtils.showShort("请输入正确的手机号码");
            return;
        }
        UserServiceFactory.userByPhone(phone)
                .subscribe(new AbstractDialogSubscriber<UserInfoBean>(this) {
                    @Override
                    public void onNext(UserInfoBean userInfoBean) {
                        dismissLoadingDialog();
                        invite(userInfoBean.getUserUuid());
                    }
                });
    }

    private void invite(String uuid){
        UserServiceFactory.inviteUser(uuid,"INVITE_USER")
                .subscribe(new AbstractDialogSubscriber<String>(this) {
                    @Override
                    public void onNext(String s) {
                        dismissLoadingDialog();
                    }
                });
    }
}
